package android.team.app.network;

import java.io.IOException;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

import android.content.Context;
import android.team.app.info.WebserviceConfig;

public class AsyncHttpPost extends AsyncHttpBase {

	public AsyncHttpPost(Context context, AsyncHttpResponseListener listener,
			List<NameValuePair> params) {
		super(context, listener, params);
		// TODO Auto-generated constructor stub
	}

	public AsyncHttpPost(Context context,
			AsyncHttpResponseProcess httpResponseProcess,
			List<NameValuePair> params) {
		super(context, httpResponseProcess, params);
	}

	@Override
	protected String request(String url) {
		try {
			HttpParams params = new BasicHttpParams();
			HttpConnectionParams.setConnectionTimeout(params,
					WebserviceConfig.NETWORK_TIMEOUT);
			HttpConnectionParams.setSoTimeout(params,
					WebserviceConfig.NETWORK_TIMEOUT);
			HttpClient httpClient = createHttpClient(params);
			HttpPost httpPost = new HttpPost(url);
			httpPost.setEntity(new UrlEncodedFormEntity(parameters));

			try {
				response = httpClient.execute(httpPost);
				statusCode = STATUS_NETWORK_OK;

			} catch (java.net.SocketTimeoutException e) {
				statusCode = STATUS_NETWORK_TIMEOUT;
			} catch (javax.net.ssl.SSLException e) {
				statusCode = STATUS_NETWORK_EXCEIPTION;
			} catch (org.apache.http.conn.ConnectTimeoutException e) {
				statusCode = STATUS_NETWORK_TIMEOUT;
			} catch (ClientProtocolException e) {
				statusCode = STATUS_NETWORK_ERROR;
			} catch (IOException e) {
				statusCode = STATUS_NETWORK_ERROR;
			} catch (Exception e) {
				statusCode = STATUS_NETWORK_EXCEIPTION;
			}

		} catch (Exception e) {
			statusCode = STATUS_NETWORK_EXCEIPTION;
		}
		return super.request(url);
	}

}
